
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    table {
      width: 500px;
      margin: 30px auto;
      text-align: center;
    }
  </style>
</head>
<body>
<button></button>
  

  <script>
 
 var arr = [ { name: 'Jack', age: 18, gender: '男' },{ name: 'Rose', age: 20, gender: '女' },{ name: 'Top', age: 22, gender: '男' } ]

    
 
   
     
            // 1、创建a标签的元素节点
            var aDom = document.createElement('table');
            document.body.appendChild(aDom);
            var bDom = document.createElement('tr');
            aDom.appendChild(bDom);


            var cDom = document.createElement('th');
            var dDom = document.createElement('th');
            var eDom = document.createElement('th');
            var fDom = document.createElement('th');
            var textNode = document.createTextNode('姓名')
            cDom.appendChild(textNode)
            var textNode1 = document.createTextNode('性别')
            dDom.appendChild(textNode1)
            var textNode2 = document.createTextNode('年龄')
            eDom.appendChild(textNode2)
            var textNode3 = document.createTextNode('操作')
            fDom.appendChild(textNode3)
            bDom.appendChild(cDom);
            bDom.appendChild(dDom);
            bDom.appendChild(eDom);
            bDom.appendChild(fDom);
            
            
            document.querySelector('button').onclick = function(){
            arr.forEach(function (item) {
        var dd = document.createElement('tr')
            aDom.appendChild(dd);   
   
            var cDom = document.createElement('td');
            var dDom = document.createElement('td');
            var eDom = document.createElement('td');
           
            var textNode = document.createTextNode(item.name)
            cDom.appendChild(textNode)
            var textNode1 = document.createTextNode(item.age)
            dDom.appendChild(textNode1)
            var textNode2 = document.createTextNode(item.gender)
            eDom.appendChild(textNode2)
            dd.appendChild(cDom);
            dd.appendChild(dDom);
            dd.appendChild(eDom);



            var gDom = document.createElement('button');
            // 2、创建属性节点
            var attrNode1 = document.createAttribute('class');
            // 3、设置属性节点的内容
            attrNode1.nodeValue = 'delete';
            // 4、将属性节点设置到a标签上  setAttributeNode为节点设置属性节点 传递属性节点对象
            gDom.setAttributeNode(attrNode1);
            
           
            var attrNode = document.createAttribute('onclick');
            // 3、设置属性节点的内容
            attrNode.nodeValue = 'deleteTr(this)';
            // 4、将属性节点设置到a标签上  setAttributeNode为节点设置属性节点 传递属性节点对象
            gDom.setAttributeNode(attrNode);
            // 5、创建文本节点
            var textNode = document.createTextNode('删除')
            // 6、将文本节点设置为aDom的子节点
            gDom.appendChild(textNode)
            dd.appendChild( document.createElement('td').appendChild(gDom));        

            
            }
            )


    }
/*     function deleteTr(obj){
            // 获取到当前点击的dom对象对应的父级tr对象
            // parentElement表示获取到上一级的dom对象
            // 传递的参数obj 是当前的dom对象
            // console.log(obj.parentElement.parentElement)
            var trDom = obj.parentElement.parentElement.parentElement;
            document.querySelector('table').removeChild(trDom);
        } */

        function deleteTr(ele){
    ele.parentNode.parentNode.removeChild(ele.parentNode)
}

/*     document.querySelectorAll('.delete').forEach(function(dom){
      deleteTr.onclick = function(){
          console.log('delete');
        }
      }) */
    
  </script>
</body>
</html>
